package edu.chalmers.gos;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import edu.chalmers.gos.Cards.Card;
import edu.chalmers.targine.Arbject;
import edu.chalmers.targine.TextBillboard;

public class CardArbject extends Arbject{
	private Card card = null;
	private Card pendingCard = null;
	private int money = 0;
	private int initiative = 0;
	private int victoryPoints = 0;
	private String ownerMAC;
	private boolean kneel = false;
	
	private TextBillboard title;
	private TextBillboard extra;
	
	private static int quadTexture = -1;
	private static int redTex = -1;
	private static int greenTex = -1;
	private static int blueTex = -1;
	
	public CardArbject(String name, String patternID, String nfcID, String owner, Context context) {
		super(name, patternID, nfcID, context);
		//
		title = new TextBillboard(mContext, "", 12.5f, -20f, -1f, 30f, 180f);
		extra = new TextBillboard(mContext, "", 12.5f, 0f, -2f, 30f, 180f);
		addTextBillboard(title);
		addTextBillboard(extra);
		//
		this.ownerMAC = owner;
	}

	public void setCard(Card newCard){
		if(resetModels){
			pendingCard = newCard;
		} else{
			card = newCard;
		}
		markForReinitialization();
	}
	
	@Override
	public void markForReset(){
		pendingCard = null;
		super.markForReset();
	}
	
	public Card getCard(){
		return card;
	}
	
	public boolean isVacant(){
		return card == null;
	}
	
	public boolean hasType(String type){
		return card.type.equals(type);
	}
	
	public boolean hasColor(String color){
		if(color.equals(Card.RED)){
			return card.red;
		} else if(color.equals(Card.GREEN)){
			return card.green;
		} else if(color.equals(Card.BLUE)){
			return card.blue;
		} else{
			return false;
		}
	}
	
	public boolean isOwnedBy(String playerMAC){
		return ownerMAC.equals(playerMAC);
	}
	
	public void addMoney(int change){
		money += change;
		extra.setText("money:" + money + ", vp:" + victoryPoints);
		markForReinitialization();
	}
	
	public void subtractMoney(int change){
		money -= change;
		extra.setText("money:" + money + ", vp:" + victoryPoints);
		markForReinitialization();
	}
	
	public void setMoney(int change){
		money = change;
		extra.setText("money:" + money + ", vp:" + victoryPoints);
		markForReinitialization();
	}
	
	public int getMoney(){
		return money;
	}
	
	public void setInitiative(int change){
		initiative = change;
	}
	public int getInitiative(){
		return initiative;
	}
	
	public int pickVP(int amount){
		if(victoryPoints >= amount){
			victoryPoints -= amount;
			extra.setText("money:" + money + ", vp:" + victoryPoints);
			markForReinitialization();
			return amount;
		} else if(victoryPoints > 0){
			int temp = victoryPoints;
			victoryPoints = 0;
			extra.setText("money:" + money + ", vp:" + victoryPoints);
			markForReinitialization();
			return temp;
		}
		return 0;
	}
	
	public void addVP(int newVictoryPoints){
		victoryPoints += newVictoryPoints;
		extra.setText("money:" + money + ", vp:" + victoryPoints);
		markForReinitialization();
	}
	
	public boolean getKneel(){
		return kneel;
	}
	
	public void setKneel(boolean kneel){
		this.kneel = kneel;
		if(kneel){
			title.xRot = 90f;
		} else{
			title.xRot = 180f;
		}
	}
	
	@Override
	public void init(GL10 gl){
		//load card textures
		if(quadTexture == -1){
			quadTexture = loadTexture(gl, mContext, R.drawable.card_bd);
			redTex = loadTexture(gl, mContext, R.drawable.icon_red);
			greenTex = loadTexture(gl, mContext, R.drawable.icon_green);
			blueTex = loadTexture(gl, mContext, R.drawable.icon_blue);
		}
		
		if(card != null){
			if(card.type.equals(Card.RESOURCES)){
				title.setText("Resources");
				extra.setText("money:" + money + ", vp:" + victoryPoints);
				
				addBillboard(quadTexture, 0f, 0f, 0f, 60f, 90f);
			} else{
				title.setText(card.name);
				extra.setText("" + card.power);
				
				addBillboard(quadTexture, 0f, 0f, 0f, 60f, 90f);
				if(hasColor(Card.RED)){
					addBillboard(redTex, -25f, 10f, -1f, 5f, 90f);
				} if(hasColor(Card.GREEN)){
					addBillboard(greenTex, -25f, 20f, -1f, 5f, 90f);
				} if(hasColor(Card.BLUE)){
					addBillboard(blueTex, -25f, 30f, -1f, 5f, 90f);
				}
			}
		}
		
		super.init(gl);
	}
	
	@Override
	public void draw(GL10 gl){
		super.draw(gl);
		
		if(pendingCard != null){
			setCard(pendingCard);
			pendingCard = null;
		}
	}
	
	@Override
	public void clear(){
		super.clear();
		card = null;
		title.setText("");
		extra.setText("");
		addTextBillboard(title);
		addTextBillboard(extra);
	}
}